﻿package nl.sanity.display 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	
	import nl.sanity.debug.LogConsole;
	
	/**
	 * Background
	 *
	 * @langversion ActionScript 3
	 *
	 * @author M.P. Bartnig
	 * @since  17/03/2009
	 */
	public class Background extends Sprite
	{
		public static const NAME			:String	= "Background";
		
		public static const REPEAT_DEFAULT	:String	= "repeatDefault";
		public static const REPEAT_X		:String	= "repeatX";
		public static const REPEAT_Y		:String	= "repeatY";
		public static const REPEAT_NO		:String	= "repeatNO";
		
		private var _image					:*;
		private var _bmp					:Bitmap;
		private var _repeat					:String;
		private var _blend					:Boolean;
		private var _fastDraw				:Boolean;
		
		
		// --- CONSTRUCTOR ----------------------------------------------
		
		public function Background( image:*, repeat:String = Background.REPEAT_DEFAULT, blendMode:Boolean = false ) 
		{
			_image		= image;
			_repeat		= repeat;
			blend		= blendMode;
			_fastDraw	= false;
			
			stage ? init() : addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		// --- PROTECTED METHODS ----------------------------------------------
		
		protected function init( event:Event = null ):void 
		{
			LogConsole.info( NAME + ".init" );
			
			removeEventListener( Event.ADDED_TO_STAGE, init );
	
			setupStageProperties();	
			createBackgroundFill( _image );
		}
		
		protected function createBackgroundFill( image:* ):void
		{
			LogConsole.info( NAME + ".setupTile | image=" + image );
			
			_bmp 	= new Bitmap();
			_image	= image;
			
			if ( _image is BitmapData )
			{
				createFillFromClass();
			}
			else if ( _image is String )
			{
				createFillFromFile();
			}
			else
			{
				throw new Error( "No bitmapData or imagepath found..." );
			}
		}
		
		protected function createFillFromClass():void
		{
			_bmp.bitmapData = _image;
			
			if ( _fastDraw )
			{
				redrawFast();
			}
			else
			{
				redraw();
			}
		}
		
		protected function createFillFromFile():void
		{
			var loader:Loader	= new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onImageLoadComplete );
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onImageLoadError );
			loader.load( new URLRequest( _image ) );
		}
		
		protected function setupStageProperties():void
		{
			LogConsole.info( NAME + ".setupStageProperties" );
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align		= StageAlign.TOP_LEFT;
			stage.addEventListener( Event.RESIZE, redraw );
			stage.addEventListener( Event.REMOVED_FROM_STAGE, clear );
		}
		
		protected function redraw( event:Event = null ):void 
		{
			LogConsole.info( NAME + ".redraw" );
			
			if ( _fastDraw )
			{
				return;
			}
			
			switch ( _repeat )
			{
				case Background.REPEAT_X:
					graphics.clear()
					graphics.beginBitmapFill( _bmp.bitmapData );
					graphics.drawRect( 0, 0, stage.stageWidth, _bmp.height );
					graphics.endFill();
					break;
					
				case Background.REPEAT_Y:
					graphics.clear()
					graphics.beginBitmapFill( _bmp.bitmapData );
					graphics.drawRect( 0, 0, _bmp.width, stage.stageHeight );
					graphics.endFill();
					break;
					
				case Background.REPEAT_NO:
					graphics.clear()
					graphics.beginBitmapFill( _bmp.bitmapData );
					graphics.drawRect( 0, 0, _bmp.width, _bmp.height );
					graphics.endFill();
					break;
					
				case Background.REPEAT_DEFAULT:	
				default:
					graphics.clear()
					graphics.beginBitmapFill( _bmp.bitmapData );
					graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
					graphics.endFill();
					break;
			}
			
			x = 0;
			y = 0;
		}
		
		protected function redrawFast():void
		{
			LogConsole.info( NAME + ".redrawFast" );
			
			graphics.clear()
			graphics.beginBitmapFill( _bmp.bitmapData );
			graphics.drawRect( 0, 0, Capabilities.screenResolutionX * 1.25, Capabilities.screenResolutionY * 1.25 );
			graphics.endFill();
		}
		
		protected function clear( event:Event = null ):void 
		{
			stage.removeEventListener( Event.RESIZE, redraw );
			stage.removeEventListener( Event.REMOVED_FROM_STAGE, clear );
			
			if ( _bmp )
			{
				_bmp.bitmapData.dispose();
			}
		}
		
		// --- PRIVATE METHODS ----------------------------------------------
		
		private function onImageLoadComplete( event:Event ):void 
		{
			LogConsole.info( NAME + ".onImageLoadComplete" );
			
			_bmp.bitmapData = Bitmap( event.target.content ).bitmapData;
			
			if ( _fastDraw )
			{
				redrawFast();
			}
			else
			{
				redraw();
			}
		}
		
		private function onImageLoadError( event:IOError ):void 
		{
			LogConsole.info( NAME + ".onImageLoadError | An error occured while loading image. Please verify the image location." );
		}
		
		// --- GETTERS / SETTERS ----------------------------------------------
		
		public function get repeat():String
		{
			return _repeat;
		}
		
		public function set repeat( value:String ):void 
		{
			_repeat = value;
			redraw();
		}
		
		public function get blend():Boolean
		{
			return _blend;
		}
		
		public function set blend( value:Boolean ):void 
		{
			_blend = value;
			
			if ( _blend )
			{
				blendMode = BlendMode.MULTIPLY;
			}
			else
			{
				blendMode = BlendMode.NORMAL;
			}
		}
		
		public function get fastDraw():Boolean
		{
			return _fastDraw;
		}
		
		public function set fastDraw( value:Boolean ):void 
		{
			_fastDraw = value;
		}

	}
	
}